package com.ja.openchat.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ja.openchat.AppViewModelFactory;
import com.ja.openchat.BR;
import com.ja.openchat.BuildConfig;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentLoginByPhoneBinding;
import com.ja.openchat.databinding.FragmentLoginPasswordBinding;
import com.ja.openchat.viewmodels.LoginByPhoneViewModel;
import com.ja.openchat.viewmodels.LoginViewModel;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.config.AppConfigInfo;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.KLog;

public class LoginByPasswordFragment extends BaseFragment<FragmentLoginPasswordBinding, LoginViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_login_password;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public LoginViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(LoginViewModel.class);
    }

    @Override
    public void initViewObservable() {
        binding.versionStr.setText("varsion "+ BuildConfig.VERSION_NAME);
//        binding.versionStr.setText("varsion "+BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE);
        //监听ViewModel中pSwitchObservable的变化, 当ViewModel中执行【uc.pSwitchObservable.set(!uc.pSwitchObservable.get());】时会回调该方法
        viewModel.uc.pSwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (viewModel.uc.pSwitchEvent.getValue()) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    binding.ivSwichPasswrod.setImageResource(R.drawable.setting_pwd_visible);
                    binding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //密码不可见
                    binding.ivSwichPasswrod.setImageResource(R.drawable.setting_pwd_invisble);
                    binding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        viewModel.uc.showLoginByPhoneEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (viewModel.uc.showLoginByPhoneEvent.getValue()) {
                    //验证码登陆按钮可见
                    binding.tvLoginByPhone.setVisibility(View.VISIBLE);
                } else {
                    //验证码登陆按钮不可见
                    binding.tvLoginByPhone.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {
            String country = data.getStringExtra("country");
            KLog.d(country);
            viewModel.setCountry(country);
        }
    }


    @Override
    public boolean isBackPressed() {
        clearData();
        return super.isBackPressed();
    }

    public void clearData(){
        AppConfigInfo appConfigInfo = AppConfigInfo.getAppWithoutLoginConfigInfo();
        if(appConfigInfo == null || !appConfigInfo.phone_code_login){
            TelegramManager.getTelegramManager().cleanCurrentData();
        }
    }

}